<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
        <h1 v-if="fromType===1">登录</h1>
        <h1 v-else-if="fromType===2" style="margin-left:50px">注册</h1>
        <h1 v-else>短信快捷登录</h1>
      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <!-- el-form > el-form-item > el-input -->
        <el-form ref="form" v-show="fromType===1" :model="loginForm" :rules="loginRules">

          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" show-password placeholder="请输入密码" />
          </el-form-item>
          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 350px" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>

        <!-- 注册 -->
        
      <el-form ref="register" v-show="fromType === 2" :label-width="labelWidth" :model="registerForm"  :rules="registerRules">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="registerForm.username" placeholder="请输入用户名"/>
    </el-form-item>

    <el-form-item label="昵称" prop="name">
      <el-input v-model="registerForm.name" placeholder="请输入昵称"/>
    </el-form-item>
    
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="registerForm.phone" placeholder="请输入手机号"></el-input>
    </el-form-item>
    
    <el-form-item label="性别">
      <el-radio-group v-model="registerForm.gender">
        <el-radio label="0">女</el-radio>
        <el-radio label="1">男</el-radio>
      </el-radio-group>
    </el-form-item>
    
    <el-form-item label="考生类型">
      <el-radio-group v-model="registerForm.type">
        <el-radio label="1">理科</el-radio>
        <el-radio label="2">文科</el-radio>
        <el-radio label="3">艺术生</el-radio>
      </el-radio-group>
    </el-form-item>
    
    <el-form-item label="成绩" prop="score">
      <el-input v-model="registerForm.score" placeholder="请输入成绩"></el-input>
    </el-form-item>
    
    <el-form-item label="密码" prop="password">
      <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
    </el-form-item>
    
    <el-form-item label='确认密码' prop="rePassword">
      <el-input v-model="registerForm.rePassword" type="password" placeholder="确认密码"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="register()" style="width: 350px;">注册</el-button>
    </el-form-item>
  </el-form>

        <div class="other-actions">
          <el-button type="text" v-if="fromType===1" @click="()=>{this.fromType=3}">短信快捷登录</el-button>
          <el-button type="text" v-if="fromType===3||fromType===2" @click="()=>{this.fromType=1}">返回登录</el-button>
          <el-button type="text" v-if="fromType===1||fromType===3" @click="()=>this.fromType=2" >注册</el-button>
        </div>

      </el-card>
    </div>
  </div>
</template>
<script>
import{userRegister} from '@/api/user'
export default {
  name: 'Login',
  data() {
    return {
      fromType:1,//表单类型  1表示登录表单  2注册表单 3短信登录表单
      loginForm: {
        username: '',
        password: '',
        isAgree: false
      },
      labelWidth:"100px"
      ,registerForm: {//注册表单
        username: '',
        name: '',
        phone: '',
        gender: '0',//默认是女生
        type: '1',//默认是理科
        score: '',
        password: '',
        rePassword: ''
      }
      ,
      loginRules: {
        username: [  //必填项
           { required: true, message: '请输入用户名', trigger: 'blur' },
           {min: 6,max: 15,message: '用户名长度应该为6-15位之间', trigger:'blur'}],
        password: [{
          required: true,
          message: '请输入密码',
          trigger: 'blur'
        }, {
          min: 6,
          max: 16,
          message: '密码长度应该为6-16位之间',
          trigger: 'blur'

        }],
        // required只能检查 null "" undefined
        isAgree: [{
          validator: (rule, value, callback) => {
            // rule规则
            // value检查的数据 true/false
            // callback 函数 执行这个函数
            // 成功执行callback 失败也执行callback(错误对象 new Error(错误信息))
            value ? callback() : callback(new Error('没有勾选用户平台协议'))
          }
        }]
      },
      //注册表单验证规则
      registerRules: {
        username: [
          //必填项
           { required: true, message: '请输入用户名', trigger: 'blur' },
           {min: 6,max: 15,message: '用户名长度应该为6-15位之间', trigger:'blur'}
        ],
        name:[
           { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        phone:[
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^\d{11}$/, message: '手机号格式不正确', trigger: 'blur' }
          ],
        score:[
          {required:true,message:'请输入成绩',trigger:'blur'},
          {validator:(rule,value,callback)=>{
            const score = parseInt(value);
            if (isNaN(score) || score < 0 || score > 750) {
                callback(new Error('成绩应在0到750之间'));
            } else {
              callback();
              }
          },trigger:'blur'}],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度在6到15位之间', trigger: 'blur' }
          ],
        rePassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: (rule,value,callback)=>{
            if (value !== this.registerForm.password) {
              callback(new Error('两次输入的密码不一致'));
            } else {
             callback();
            }
          }, trigger: 'blur' }
        ]
    }
    }},
  methods: {
    login() {
      //整个表单校验
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          //校验通过
          //vuex存储共享数据
          await this.$store.dispatch('user/login', this.loginForm).then((res)=>{
            if(res.code===1){
               this.$router.push('/') //登录成功跳转到首页
            }else{
              this.$message.error(res.msg)
            }
          }).catch(()=>{
            this.$message.error("账号或密码错误")
          })
        }
      })
    },
    register(){
      this.$refs.register.validate( (valid) =>{
        if(valid) {
          userRegister(this.registerForm).then((res)=>{
            if(res.data.code===1){
              this.$message.success('注册成功')
               this.registerForm={}//清空表单
               this.loginForm={}
              this.loginForm.mobile=this.registerForm.phone
              this.fromType=1 //切换登录表单
            }
          })
    
        }
      })
}
  }
}
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/beijing.jpg)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/background.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 100px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
   
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color: #606266;
    }
  }
  .other-actions {
    display: flex;
    justify-content: space-between;
    margin-top: 20px;
    margin-right: 80px;
    el-button {
      color: #606266;
    }
  }
}
</style>